<?php
/**
 * @var $this NewController
 * @var $cs   CClientScript
 */

$cs = Yii::app()->clientScript;

// include libs
$cs->registerScriptFile('/js/lib/json2.min.js');
$cs->registerScriptFile('/js/lib/underscore-min.js');
$cs->registerScriptFile('/js/lib/backbone-min.js');
$cs->registerScriptFile('/js/lib/backbone.localStorage-min.js');
//$cs->registerScriptFile('/js/lib/backbone.dualStorage-min.js');

// include main file
$cs->registerScriptFile('/js/nTodo/Todo.js', CClientScript::POS_END);

// include models
$cs->registerScriptFile('/js/nTodo/Model/Item.js', CClientScript::POS_END);
$cs->registerScriptFile('/js/nTodo/Model/Category.js', CClientScript::POS_END);

//include collections
$cs->registerScriptFile('/js/nTodo/Collection/Base.js', CClientScript::POS_END);
$cs->registerScriptFile('/js/nTodo/Collection/Item.js', CClientScript::POS_END);
$cs->registerScriptFile('/js/nTodo/Collection/Category.js', CClientScript::POS_END);

//include views
$cs->registerScriptFile('/js/nTodo/View/NewItem.js', CClientScript::POS_END);
$cs->registerScriptFile('/js/nTodo/View/Item.js', CClientScript::POS_END);
$cs->registerScriptFile('/js/nTodo/View/Category.js', CClientScript::POS_END);
$cs->registerScriptFile('/js/nTodo/View/App.js', CClientScript::POS_END);

$cs->registerScript('todo.init', 'Todo.App = new Todo.View.App(); Todo.App.render();');

$this->pageTitle = Yii::app()->name;
?>

<div id="toddo-app">
    <header>
        <div class="inpad">
            <span class="user">
                <?php
                $this->renderPartial('//partials/auth');
                ?>
            </span>

            <h1>TODDO</h1>
        </div>
    </header>

    <article>
        <div id="notification" style="display: none"></div>
        <section id="newitem">
            <div class="input">
                <input id="mic" type="text" x-webkit-speech="x-webkit-speech" lang="ru"/>
                <textarea id="name" placeholder="Посадить дерево" autofocus="autofocus" rows="1"
                          tabindex="1"></textarea>
                <a href="#" class="clear"></a>
            </div>

            <span id="clone"></span>
            <span id="return"></span>
        </section>
        <ul id="lists"></ul>
        <div class="buffer"></div>
    </article>
</div>

<script type="text/javascript">
    <?php
    $storageKey = md5(Yii::app()->user->service . Yii::app()->user->id);
    ?>
    var storageKey = <?php echo json_encode($storageKey) ?>;
</script>

<script type="text/template" id="category-template">
    <% if(id != 0 && id != null && name) { %>
        <p class="title"><span class="drag"></span><%- name.replace(/@.+/, '') %></p>
    <% } %>
    <% if(false) { //disabled %>
        <div class="title">
            <textarea><%= name %></textarea>
            <span><%- name.replace(/@.+/, '') %></span>
    </div>
    <% } %>

    <% _ . each(['due', 'date', 'other'], function (type) { %>
        <ul class="<%= type %>"></ul>
    <% }) %>
</script>

<script type="text/template" id="item-template">
    <% if (!deleted) { %><!--
            --><p><!--
                --><span class="drag"></span><!--
                --><label class="toggleCheck"><em class="check"></em></label><!--
                --><span class="name"><%- name .replace(/#([\d]{2})\/([\d]{2})$/, '').replace(/#([\d]{2})\/([\d]{2})/g, '$1/$2').replace(/@.+/, '') %></span><!--
                --><em><%- deadline ? deadline : '' %></em><!--
            --></p><!--
            --><p class="edit"><!--
                --><button><em class="delete"></em></button><!--
                --><textarea rows="1" style="width: 460px;"><%= name %></textarea><!--
            --></p><!--
    --><% } %>
</script>

<script type="text/template" id="notification-error-sync">
    <div class="message">
        <p>
            <% if (parseInt(timeLeft) > 0) { %>
            Синхронизация не производится.
            Соединение через <%= timeLeft %> с.
            <a href="javascript: void(0)" class="resync-link">Повторить</a>
            <% } else { %>
            Попытка соединения с сервером...
            <% } %>
        </p>
    </div>
</script>